package app.sys.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author fyy
 * @description: 图片工具类
 */
public class FileUtil {
    //上传图片
    public static String uploadFile(MultipartFile multipartFile, String basePath,String fileName) {
        // 获取绝对路径
        File f = new File(basePath);
        // 如果不存在，直接创建
        if (!f.exists()) {
            f.mkdirs();
        }
        // 拼接的图片路径
        String filepath = basePath + fileName;
        File file = new File(filepath);
        // 上传图片
        try {
            multipartFile.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return filepath;
    }

    // 回显图片
    public static void showFile(String fielUrl, HttpServletResponse response) {
        // 获取图片的当前路径 放入读
        FileInputStream fis = null;
        // 用response 获取一个写对象的流
        ServletOutputStream os = null;
        try {
            fis = new FileInputStream(fielUrl);
            fielUrl.getBytes();
            os = response.getOutputStream();
            // 提高读写的速度
            byte[] b = new byte[1024];
            // 边读边写
            while (fis.read(b) != -1) {
                os.write(b);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 下载
    public static void downLoad(String filePath, HttpServletRequest request, HttpServletResponse response) {
        //设置文件的MiMe类型
        response.setContentType(request.getSession().getServletContext().getMimeType(filePath));
        //设置content-disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + filePath);
        //读取目标文件，通过response将目标文件写到客户
        try {
            // 读取文件
            InputStream in = new FileInputStream(filePath);
            OutputStream out = response.getOutputStream();
            //写文件
            byte[] b = new byte[1024];
            while (in.read(b) != -1) {
                out.write(b);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 根据文件扩展名获取Content-Type
    public static String getContentType(String extension) {
        if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {
            return "image/jpeg";
        } else if ("png".equalsIgnoreCase(extension)) {
            return "image/png";
        } else if ("gif".equalsIgnoreCase(extension)) {
            return "image/gif";
        } else if ("txt".equalsIgnoreCase(extension)) {
            return "text/plain";
        } else if ("pdf".equalsIgnoreCase(extension)) {
            return "application/pdf";
        } else if ("doc".equalsIgnoreCase(extension) || "docx".equalsIgnoreCase(extension)) {
            return "application/msword";
        } else if ("xls".equalsIgnoreCase(extension) || "xlsx".equalsIgnoreCase(extension)) {
            return "application/vnd.ms-excel";
        } else if ("ppt".equalsIgnoreCase(extension) || "pptx".equalsIgnoreCase(extension)) {
            return "application/vnd.ms-powerpoint";
        } else if ("zip".equalsIgnoreCase(extension)) {
            return "application/zip";
        } else if ("tar".equalsIgnoreCase(extension)) {
            return "application/x-tar";
        } else if ("rar".equalsIgnoreCase(extension)) {
            return "application/x-rar-compressed";
        } else if ("gz".equalsIgnoreCase(extension)) {
            return "application/gzip";
        } else {
            return "application/octet-stream";
        }
    }
}
